﻿using AutoMapper;
using B.S.Filtration.Domain;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Read.Api.Application.Command.FiltrateCommand;
using MediatR;
using SqlSugar;
using static NPOI.HSSF.Util.HSSFColor;

namespace B.S.Filtration.Read.Api.Application.CommandHander.FiltrateCommandHander
{
    public class QueryFiltrateCommandHander:IRequestHandler<QueryFiltrateCommand,APIPaging<List<Filtrate>>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public QueryFiltrateCommandHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public async Task<APIPaging<List<Filtrate>>> Handle(QueryFiltrateCommand request, CancellationToken cancellationToken)
        {
            APIPaging<List<Filtrate>> result = new APIPaging<List<Filtrate>>();

            var item = await db.Queryable<Filtrate>().Where(x => x.Del == false).ToListAsync();

            result.TotalCount = item.Count();
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
            item = item.OrderByDescending(x => x.FiltrateId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

            result.code = APIResultCode.success;
            result.msg = "查询交接班信息成功";
            result.data = item;

            return result;
        }
    }
}
